/*
 * Copyright 2011 www.pretty-tools.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

function yrprxResourceBundle(url)
{
    if (!url) throw "URL to resource file is missing";

    try
    {
        if (url.indexOf("chrome") == 0) //it's FireFox URL
        {
            var LOCALE = "/locale/";
            var idx = url.indexOf(LOCALE);
            if (idx == -1)
                throw "Unsupported URL format: " + url;
            url = url.substring(idx + LOCALE.length - 1);
        }

        var xhr;
        try
        {
            xhr = new XMLHttpRequest();
            xhr.open("GET", chrome.extension.getURL("_locales/" + chrome.i18n.getMessage("@@ui_locale") + url), false);
            xhr.send();
        }
        catch(e)
        {
            xhr = new XMLHttpRequest();
            xhr.open("GET", chrome.extension.getURL("_locales/en/" + url), false);
            xhr.send();
        }

        var lines = xhr.responseText.split("\n");
        for (var i in lines)
        {
            var line = lines[i].trim();

            if (line.length == 0)
                continue;

            if (line.indexOf("#") == 0)
                continue;

            var j = line.indexOf("=");
            if (j == -1 || (j == line.length -1))
                continue;

            this[line.substring(0, j)] = line.substring(j+1);
        }

    }
    catch(ex)
    {
        try
        {
            var logger = yrprxFactory.createLogger("yrprxResourceBundle");

            if (logger.enabled) logger.error("Exception: " + ex);
        }
        catch(ex)
        {
            console.log(ex);
        }
    }
}

yrprxResourceBundle.prototype.getString = function(name)
{
    return this[name];
}

var wsshResources = new yrprxResourceBundle("/Resources.properties");
